{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mathematical functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.11.2'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "__author__ = \"kyubyong. kbpark.linguist@gmail.com. https://github.com/kyubyong\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trigonometric functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q1. Calculate sine, cosine, and tangent of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sine: [ 0.          0.84147098 -0.98803162  0.89399666]\n",
      "cosine: [ 1.          0.54030231  0.15425145 -0.44807362]\n",
      "tangent: [ 0.          1.55740772 -6.4053312  -1.99520041]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([0., 1., 30, 90])\n",
    "print \"sine:\", np.sin(x)\n",
    "print \"cosine:\", np.cos(x)\n",
    "print \"tangent:\", np.tan(x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q2. Calculate inverse sine, inverse cosine, and inverse tangent of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inverse sine: [ 1.57079633  0.          1.57079633]\n",
      "inverse cosine: [ 0.          1.57079633  0.        ]\n",
      "inverse tangent: [ 0.78539816  0.          0.78539816]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-1., 0, 1.])\n",
    "print \"inverse sine:\", np.arcsin(x2)\n",
    "print \"inverse cosine:\", np.arccos(x2)\n",
    "print \"inverse tangent:\", np.arctan(x2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q3. Convert angles from radians to degrees."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-180.  -90.   90.  180.]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])\n",
    "\n",
    "out1 = np.degrees(x)\n",
    "out2 = np.rad2deg(x)\n",
    "assert np.array_equiv(out1, out2)\n",
    "print out1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q4. Convert angles from degrees to radians."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-3.14159265 -1.57079633  1.57079633  3.14159265]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-180.,  -90.,   90.,  180.])\n",
    "\n",
    "out1 = np.radians(x)\n",
    "out2 = np.deg2rad(x)\n",
    "assert np.array_equiv(out1, out2)\n",
    "print out1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hyperbolic functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q5. Calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.17520119  0.          1.17520119]\n",
      "[ 1.54308063  1.          1.54308063]\n",
      "[-0.76159416  0.          0.76159416]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-1., 0, 1.])\n",
    "print np.sinh(x)\n",
    "print np.cosh(x)\n",
    "print np.tanh(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rounding"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q6. Predict the results of these, paying attention to the difference among the family functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2.  2.  2.  3. -2. -2. -3.]\n",
      "[ 2.  1.  2.  2. -3. -3. -3.]\n",
      "[ 3.  2.  3.  3. -2. -2. -2.]\n",
      "[ 2.  1.  2.  2. -2. -2. -2.]\n",
      "[2.0, 2.0, 3.0, 3.0, -2.0, -3.0, -3.0]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([2.1, 1.5, 2.5, 2.9, -2.1, -2.5, -2.9])\n",
    "\n",
    "out1 = np.around(x)\n",
    "out2 = np.floor(x)\n",
    "out3 = np.ceil(x)\n",
    "out4 = np.trunc(x)\n",
    "out5 = [round(elem) for elem in x]\n",
    "\n",
    "print out1\n",
    "print out2\n",
    "print out3\n",
    "print out4\n",
    "print out5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q7. Implement out5 in the above question using numpy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2.  2.  3.  3. -2. -3. -3.]\n"
     ]
    }
   ],
   "source": [
    "print np.floor(np.abs(x) + 0.5) * np.sign(x) \n",
    "# Read http://numpy-discussion.10968.n7.nabble.com/why-numpy-round-get-a-different-result-from-python-round-function-td19098.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sums, products, differences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q8. Predict the results of these."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('->', 36)\n",
      "('->', array([ 6,  8, 10, 12]))\n",
      "('->', array([[10],\n",
      "       [26]]))\n",
      "\n",
      "('->', 40320)\n",
      "('->', array([ 5, 12, 21, 32]))\n",
      "('->', array([[  24],\n",
      "       [1680]]))\n",
      "\n",
      "('->', array([ 1,  3,  6, 10, 15, 21, 28, 36]))\n",
      "('->', array([[ 1,  2,  3,  4],\n",
      "       [ 6,  8, 10, 12]]))\n",
      "('->', array([[ 1,  3,  6, 10],\n",
      "       [ 5, 11, 18, 26]]))\n",
      "\n",
      "('->', array([    1,     2,     6,    24,   120,   720,  5040, 40320]))\n",
      "('->', array([[ 1,  2,  3,  4],\n",
      "       [ 5, 12, 21, 32]]))\n",
      "('->', array([[   1,    2,    6,   24],\n",
      "       [   5,   30,  210, 1680]]))\n",
      "\n",
      "('->', 1)\n",
      "('->', array([1, 2, 3, 4]))\n",
      "('->', array([[1],\n",
      "       [5]]))\n",
      "\n",
      "('->', 8)\n",
      "('->', array([5, 6, 7, 8]))\n",
      "('->', array([[4],\n",
      "       [8]]))\n",
      "\n",
      "('->', 4.5)\n",
      "('->', array([ 3.,  4.,  5.,  6.]))\n",
      "('->', array([[ 2.5],\n",
      "       [ 6.5]]))\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:34: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n"
     ]
    }
   ],
   "source": [
    "x = np.array(\n",
    "    [[1, 2, 3, 4],\n",
    "     [5, 6, 7, 8]])\n",
    "\n",
    "outs = [np.sum(x),\n",
    "        np.sum(x, axis=0),\n",
    "        np.sum(x, axis=1, keepdims=True),\n",
    "        \"\",\n",
    "        np.prod(x),\n",
    "        np.prod(x, axis=0),\n",
    "        np.prod(x, axis=1, keepdims=True),\n",
    "        \"\",\n",
    "        np.cumsum(x),\n",
    "        np.cumsum(x, axis=0),\n",
    "        np.cumsum(x, axis=1),\n",
    "        \"\",\n",
    "        np.cumprod(x),\n",
    "        np.cumprod(x, axis=0),\n",
    "        np.cumprod(x, axis=1),\n",
    "        \"\",\n",
    "        np.min(x),\n",
    "        np.min(x, axis=0),\n",
    "        np.min(x, axis=1, keepdims=True),\n",
    "        \"\",\n",
    "        np.max(x),\n",
    "        np.max(x, axis=0),\n",
    "        np.max(x, axis=1, keepdims=True),\n",
    "        \"\",\n",
    "        np.mean(x),\n",
    "        np.mean(x, axis=0),\n",
    "        np.mean(x, axis=1, keepdims=True)]\n",
    "           \n",
    "for out in outs:\n",
    "    if out == \"\":\n",
    "        print\n",
    "    else:\n",
    "        print(\"->\", out)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q9. Calculate the difference between neighboring elements, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3 -7]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 4, 7, 0])\n",
    "print np.diff(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q10. Calculate the difference between neighboring elements, element-wise, and\n",
    "prepend [0, 0] and append[100] to it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0   0   1   2   3  -7 100]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 4, 7, 0])\n",
    "\n",
    "out1 = np.ediff1d(x, to_begin=[0, 0], to_end=[100])\n",
    "out2 = np.insert(np.append(np.diff(x), 100), 0, [0, 0])\n",
    "assert np.array_equiv(out1, out2)\n",
    "print out2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q11. Return the cross product of x and y."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-3  6 -3]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 3])\n",
    "y = np.array([4, 5, 6])\n",
    "\n",
    "print np.cross(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exponents and logarithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q12. Compute $e^x$, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  2.71828175   7.38905621  20.08553696]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1., 2., 3.], np.float32)\n",
    "\n",
    "out = np.exp(x)\n",
    "print out\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q13. Calculate exp(x) - 1 for all elements in x."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.71828175   6.38905621  19.08553696]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1., 2., 3.], np.float32)\n",
    "\n",
    "out1 = np.expm1(x)\n",
    "out2 = np.exp(x) - 1.\n",
    "assert np.allclose(out1, out2)\n",
    "print out1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q14. Calculate $2^p$ for all p in x."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2.  4.  8.]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1., 2., 3.], np.float32)\n",
    "\n",
    "out1 = np.exp2(x)\n",
    "out2 = 2 ** x\n",
    "assert np.allclose(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q15. Compute natural, base 10, and base 2 logarithms of x element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "natural log = [ 0.  1.  2.]\n",
      "common log = [ 0.          0.43429448  0.86858896]\n",
      "base 2 log = [ 0.          1.44269504  2.88539008]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, np.e, np.e**2])\n",
    "\n",
    "print \"natural log =\", np.log(x)\n",
    "print \"common log =\", np.log10(x)\n",
    "print \"base 2 log =\", np.log2(x)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q16. Compute the natural logarithm of one plus each element in x in floating-point accuracy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.00000000e-099   1.00000000e-100]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1e-99, 1e-100])\n",
    "\n",
    "print np.log1p(x)\n",
    "# Compare it with np.log(1 +x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Floating point routines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q17. Return element-wise True where signbit is set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True  True False False False False]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-3, -2, -1, 0, 1, 2, 3])\n",
    "\n",
    "out1 = np.signbit(x)\n",
    "out2 = x < 0\n",
    "assert np.array_equiv(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q18. Change the sign of x to that of y, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1. -0. -1.]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-1, 0, 1])\n",
    "y = -1.1\n",
    "\n",
    "print np.copysign(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arithmetic operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q19. Add x and y element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 3])\n",
    "y = np.array([-1, -2, -3])\n",
    "\n",
    "out1 = np.add(x, y)\n",
    "out2 = x + y\n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q20. Subtract y from x element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([3, 4, 5])\n",
    "y = np.array(3)\n",
    "\n",
    "out1 = np.subtract(x, y)\n",
    "out2 = x - y \n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q21. Multiply x by y element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  0 -5]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([3, 4, 5])\n",
    "y = np.array([1, 0, -1])\n",
    "\n",
    "out1 = np.multiply(x, y)\n",
    "out2 = x * y \n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q22. Divide x by y element-wise in two different ways."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.          2.          1.66666667]\n",
      "[ 3.  2.  1.]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([3., 4., 5.])\n",
    "y = np.array([1., 2., 3.])\n",
    "\n",
    "out1 = np.true_divide(x, y)\n",
    "out2 = x / y \n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n",
    "\n",
    "out3 = np.floor_divide(x, y)\n",
    "out4 = x // y\n",
    "assert np.array_equal(out3, out4)\n",
    "print out3\n",
    "\n",
    "# Note that in Python 2 and 3, the handling of `divide` differs.\n",
    "# See https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q23. Compute numerical negative value of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  1]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, -1])\n",
    "\n",
    "out1 = np.negative(x)\n",
    "out2 = -x\n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q24. Compute the reciprocal of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.   0.5  5. ]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1., 2., .2])\n",
    "\n",
    "out1 = np.reciprocal(x)\n",
    "out2 = 1/x\n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q25. Compute $x^y$, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  4]\n",
      " [ 3 16]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1, 2], [3, 4]])\n",
    "y = np.array([[1, 2], [1, 2]])\n",
    "\n",
    "out = np.power(x, y)\n",
    "print out\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q26. Compute the remainder of x / y element-wise in two different ways."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 1 1 0 1]\n",
      "[-1  0 -1  1  0  1]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([-3, -2, -1, 1, 2, 3])\n",
    "y = 2\n",
    "\n",
    "out1 = np.mod(x, y)\n",
    "out2 = x % y\n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n",
    "\n",
    "out3 = np.fmod(x, y)\n",
    "print out3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Miscellaneous"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q27. If an element of x is smaller than 3, replace it with 3.\n",
    "And if an element of x is bigger than 7, replace it with 7."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 3 3 3 4 5 6 7 7 7]\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(10)\n",
    "\n",
    "out1 = np.clip(x, 3, 7)\n",
    "out2 = np.copy(x)\n",
    "out2[out2 < 3] = 3\n",
    "out2[out2 > 7] = 7\n",
    "\n",
    "assert np.array_equiv(out1, out2)\n",
    "print out1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q28. Compute the square of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 4 1]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, -1])\n",
    "\n",
    "out1 = np.square(x)\n",
    "out2 = x * x\n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q29. Compute square root of x element-wise.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  2.  3.]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1., 4., 9.])\n",
    "\n",
    "out = np.sqrt(x)\n",
    "print out\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q30. Compute the absolute value of x."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [3 3]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1, -1], [3, -3]])\n",
    "\n",
    "out = np.abs(x)\n",
    "print out\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q31. Compute an element-wise indication of the sign of x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  1  0 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 3, 0, -1, -3])\n",
    "\n",
    "out1 = np.sign(x)\n",
    "out2 = np.copy(x)\n",
    "out2[out2 > 0] = 1\n",
    "out2[out2 < 0] = -1\n",
    "assert np.array_equal(out1, out2)\n",
    "print out1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
